//
//  UserThirdLogin.m
//  danpin
//
//  Created by chuangjia on 12/8/15.
//  Copyright (c) 2015 chuangjia. All rights reserved.
//

#import "UserThirdLogin.h"
#import "ZToastManager.h"
#import "ZShare.h"
#import "BaseModel.h"
#import "NSString+Extensions.h"
@interface UserThirdLogin ()<UITextFieldDelegate>
{
    
}
@property (weak) id<UserDelegate> delegate;
@property(nonatomic,strong) NSDictionary     *userinfo;
@property(nonatomic,strong) UITextField     *txtUser;
@property(nonatomic,strong)void(^actionBlock)(id,id);
+ (instancetype)newController;
@end

@implementation UserThirdLogin
+ (instancetype)newController
{
    return [super new];
}

+ (instancetype)newWithDelegate:(id<UserDelegate>)delegate userinfo:(NSDictionary*)userinfo
{
    UserThirdLogin *viewController = [UserThirdLogin newController];
    
    if (viewController) {
        viewController.delegate = delegate;
        viewController.userinfo= userinfo;
    }
    
    return viewController;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
     self.view.backgroundColor = [UIColor whiteColor];
    self.rightTitle =@"完成";
    self.navTitle =@"会员昵称";
   [UMengShare event:CK_USERTHIRD acc:1];
    
    self.txtUser.frame =CGRectMake(PHOTO_FRAME_WIDTH*2, 100, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*4, PHOTO_FRAME_WIDTH*4);
    self.txtUser.text =[self.userinfo objectForKey:@"nick_name"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)dealloc
{
    self.actionBlock=nil;
    [self.txtUser resignFirstResponder];
    self.txtUser=nil;
      DLog(@"%s", __PRETTY_FUNCTION__);
}
-(void)setSelectedCallBack:(void(^)(id,id))callback
{
    self.actionBlock=callback;
}
-(void)clickNavEvent:(id)sender
{
    UIButton *btn =(UIButton*)sender;
    switch (btn.tag) {
        case 0:
        {
            [self.txtUser resignFirstResponder];
            if (self.navigationController) {
                if (self.navigationController.viewControllers.count == 1) {
                    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
                } else {
                    [self.navigationController popViewControllerAnimated:YES];
                }
            } else {
                [self dismissViewControllerAnimated:YES completion:nil];
            }
        }
            break;
        case 1:
        {
            [self okAction];
        }
            break;
        default:
            break;
    }
}
-(UITextField *)txtUser
{
    if (!_txtUser) {
        self.txtUser = [[UITextField alloc] initWithFrame:CGRectZero];
        self.txtUser.placeholder = @" 用户昵称";
        [self.txtUser setBorderStyle:UITextBorderStyleRoundedRect];
        self.txtUser.clearButtonMode = UITextFieldViewModeWhileEditing;
        self.txtUser.font = FONT(13);
        self.txtUser.autocorrectionType = UITextAutocorrectionTypeNo;
        self.txtUser.autocapitalizationType = UITextAutocapitalizationTypeNone;
        self.txtUser.returnKeyType = UIReturnKeyNext;
        self.txtUser.clearButtonMode = UITextFieldViewModeWhileEditing;
        self.txtUser.delegate=self;
        [self.txtUser becomeFirstResponder];
        [self.view addSubview:self.txtUser];
    }
    return _txtUser;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [textField resignFirstResponder];
    return YES;
}
-(void)okAction
{
    if (self.txtUser.text.length==0) {
        [[ZToastManager ShardInstance] showtoast:@"请输入昵称"];
        return;
    }
    [self.txtUser resignFirstResponder];
    [self btnLoginAction];
    
}
- (void)btnLoginAction
{
    [[ZToastManager ShardInstance] showprogress];
    __weak typeof (self) wSelf = self;
    NSMutableDictionary *dict =[NSMutableDictionary dictionary];
    [dict setValue:@"set_nick_name" forKey:@"action"];
    [dict setValue:self.txtUser.text forKey:@"nick_name"];
    [dict setValue:@"" forKey:@"email"];
    [dict setValue:@"" forKey:@"password"];
    
    [BaseModel setNickName:dict success:^(AFHTTPRequestOperation* operation, NSObject* result){
        [[ZToastManager ShardInstance] hideprogress];
        [wSelf parserResultObject:result];
    } failure:^(NSError* error){
        [[ZToastManager ShardInstance] hideprogress];
        [[ZToastManager ShardInstance] showtoast:@"请求失败"];
    }];
    
}
-(void)parserResultObject:(id)resultObject
{
    if (resultObject==nil) {
        return;
    }
    if (resultObject && [resultObject isKindOfClass:[NSDictionary class]]) {
        {
            [[ZToastManager ShardInstance] showtoast:[resultObject objectForKey:@"msg"]];
            int code =[[resultObject objectForKey:@"code"] intValue];
            if (code==1)
            {
                [[ZShare sharedInstance] initUserInfo:resultObject];
                if (_delegate&&[_delegate respondsToSelector:@selector(userLoginOK:)]) {
                    [_delegate userLoginOK:[ZShare sharedInstance].userInfo];
                }
                [[NSNotificationCenter defaultCenter] postNotificationName:@"updateLoveList" object:nil];
                
            }
        }
    }
}
@end
